package com.dhcc.DRGsHosp.application.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.dhcc.DRGsHosp.application.MDCGroupApplication;
import com.dhcc.DRGsHosp.core.domain.ADRG;
import com.dhcc.DRGsHosp.core.domain.DRGs;
import com.dhcc.DRGsHosp.core.domain.IndexCal;
import com.dhcc.DRGsHosp.core.domain.MDCGroup;
@Named
@Transactional
public class MDCGroupApplicationImpl implements MDCGroupApplication,Serializable {
	private static final long serialVersionUID = 1L;
	private QueryChannelService queryChannel;
	private EntityRepository repository;
	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice; 
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj; 
    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}
	public MDCGroup getMDCGroup(String mdcCode) {
		return MDCGroup.get(MDCGroup.class, mdcCode);
	}
	
	public void creatMDCGroup(MDCGroup mDCGroup) {
		mDCGroup.save();
	}
	
	public void updateMDCGroup(MDCGroup mDCGroup) {
		mDCGroup .save();
	}
	
	public void removeMDCGroup(MDCGroup mDCGroup) {
		if(mDCGroup != null){
			mDCGroup.remove();
		}
	}
	
	public void removeMDCGroups(Set<MDCGroup> mDCGroups) {
		for (MDCGroup mDCGroup : mDCGroups) {
			mDCGroup.remove();
		}
	}
	
	public List<MDCGroup> findAllMDCGroup() {
		return MDCGroup.findAll(MDCGroup.class);
	}

	@Override
	public List<MDCGroup> findMdcGroupList(IndexCal queryVo) {

		String pType = "0";
		if(StringUtils.hasLength(queryVo.getpType())){
			pType = queryVo.getpType();
		}
		StringBuilder jpql = new StringBuilder("select _mdcGroup from MDCGroup _mdcGroup where exists(select 1 from ArchiveData _archiveData where  _archiveData.adMdccode=_mdcGroup.mdcCode  ");
		if (queryVo.getPardepCode() != null
				&& !"".equals(queryVo.getPardepCode())) {
			jpql.append(" and _archiveData.adDepcode ='"+queryVo.getPardepCode()+"'");
		}
		if (queryVo.getPardrgCode() != null
				&& !"".equals(queryVo.getPardrgCode())) {
			jpql.append(" and _archiveData.adDrgcode ='"+queryVo.getPardrgCode()+"'");
		}
		if (queryVo.getPardepgroupCode() != null
				&& !"".equals(queryVo.getPardepgroupCode())) {
			jpql.append(" and _archiveData.adDepgroupcode ='"+queryVo.getPardepgroupCode()+"'");
		}
		if (queryVo.getParadrgCode() != null
				&& !"".equals(queryVo.getParadrgCode())) {
			jpql.append(" and _archiveData.adAdrgcode ='"+queryVo.getParadrgCode()+"'");
		}
		if (queryVo.getPargroupName() != null
				&& !"".equals(queryVo.getPargroupName())) {
			jpql.append(" and _archiveData.adGroupname ='"+queryVo.getPargroupName()+"'");
		}
		// 注释报错
//		if (queryVo.getMrLot() != null
//				&& !"".equals(queryVo.getMrLot())) {
//			jpql.append(" and _archiveData.adLot ='"+queryVo.getMrLot()+"'");
//		}
		jpql.append(" ) ");
		List<MDCGroup> mrList = getQueryChannelService()
				   .createJpqlQuery(jpql.toString())
				   .list();
		if(mrList.size() > 0){
			return mrList;
		}else{
			jpql = new StringBuilder("select _mdcGroup from MDCGroup _mdcGroup where exists(select 1 from IndexCal _IndexCal where  _IndexCal.parmdcCode=_mdcGroup.mdcCode and _IndexCal.verCode=_mdcGroup.verCode ");
			if (queryVo.getPardepCode() != null
					&& !"".equals(queryVo.getPardepCode())) {
				jpql.append(" and _IndexCal.pardepCode ='"+queryVo.getPardepCode()+"'");
			}
			if (queryVo.getPardrgCode() != null
					&& !"".equals(queryVo.getPardrgCode())) {
				jpql.append(" and _IndexCal.pardrgCode ='"+queryVo.getPardrgCode()+"'");
			}
			if (queryVo.getPardepgroupCode() != null
					&& !"".equals(queryVo.getPardepgroupCode())) {
				jpql.append(" and _IndexCal.pardepgroupCode ='"+queryVo.getPardepgroupCode()+"'");
			}
			if (queryVo.getParadrgCode() != null
					&& !"".equals(queryVo.getParadrgCode())) {
				jpql.append(" and _IndexCal.paradrgCode ='"+queryVo.getParadrgCode()+"'");
			}
			if (queryVo.getPargroupName() != null
					&& !"".equals(queryVo.getPargroupName())) {
				jpql.append(" and _IndexCal.pargroupName ='"+queryVo.getPargroupName()+"'");
			}
			// 注释报错
//			if (queryVo.getMrLot() != null
//					&& !"".equals(queryVo.getMrLot())) {
//				jpql.append(" and _IndexCal.mrLot ='"+queryVo.getMrLot()+"'");
//			}
//			if (queryVo.getMrStdtype() != null
//					&& !"".equals(queryVo.getMrStdtype())) {
////				if("month".equals(queryVo.getMrStdtype())){
////		   			jpql.append(" and _IndexCal.mrStdtype = 'period'");
////		   		}else{
//		   			jpql.append(" and _IndexCal.mrStdtype = 'year'");
////		   		}
//			}
			jpql.append(" and _IndexCal.pType ='"+pType+"'");
			jpql.append(" ) ");
			mrList = getQueryChannelService()
					   .createJpqlQuery(jpql.toString())
					   .list();
		}
		return mrList;
	}

	@Override
	public List<ADRG> findAdrgByMdcList(MDCGroup entity) {
		StringBuilder jpql = new StringBuilder("select _adrg from ADRG _adrg where _adrg.mdcCode='"+entity.getMdcCode()+"' AND  _adrg.verCode='"+entity.getVerCode()+"'");
		List<ADRG> mrList = getQueryChannelService().createJpqlQuery(jpql.toString()).list();
		return mrList;
	}
	@Override
	public List<DRGs> findDrgByMdcList(MDCGroup entity) {
		StringBuilder jpql = new StringBuilder("select _drgs from DRGs _drgs where exists (select 1 from ADRG _adrg where _adrg.mdcCode='"+entity.getMdcCode()+"' and  _adrg.adrgCode=_drgs.adrgCode) AND _drgs.verCode='"+entity.getVerCode()+"'");
		List<DRGs> mrList = getQueryChannelService().createJpqlQuery(jpql.toString()).list();
		return mrList;
	}

	@Override
	public List<DRGs> drgListByAdrg(ADRG entity) {
		StringBuilder jpql = new StringBuilder("select _drgs from DRGs _drgs where  _drgs.adrgCode='"+entity.getAdrgCode()+"' AND _drgs.verCode='"+entity.getVerCode()+"'");
		List<DRGs> mrList = getQueryChannelService().createJpqlQuery(jpql.toString()).list();
		return mrList;
	}

	@Override
	public List<MDCGroup> mdcSearch(MDCGroup entity) {
		
		String word=entity.getMdcCode();
		StringBuilder jpql = new StringBuilder("select _mdcgroup from MDCGroup _mdcgroup WHERE _mdcgroup.verCode='"+entity.getVerCode()+"' AND _mdcgroup.mdcCode like '%"+word+"%'  OR _mdcgroup.mdcDesc  like '%"+word+"%'  ");
		List<MDCGroup> mrList = getQueryChannelService().createJpqlQuery(jpql.toString()).list();
		return mrList;
	}

	@Override
	public List<MDCGroup> findAllMDCGroup(IndexCal entity) {
		String sqlStr="";
		sqlStr+="select distinct p.mdc,m.mdc_desc from  drgs.mr_ipinfo i left join drgs.t_patientdrg p on p.parid=i.pmainindex   \n";
		sqlStr+="left join drgs.t_mdcgroup m on p.ver_code=m.ver_code and p.mdc=m.mdc_code \n";
		sqlStr+="where to_char(i.P00290000,'yyyy-mm-dd') between '"+entity.getDateFrom()+"' and '"+entity.getDateTo()+"' \n";
		sqlStr+="and p.pardrg_code is not null order by p.mdc asc ";
		List<MDCGroup> mrList = getRepository().createSqlQuery(sqlStr.toString()).list();
		return mrList;
	}

	@Override
	public List<MDCGroup> queryVercode(MDCGroup entity) {
		StringBuilder jpql = new StringBuilder("select DISTINCT (_mdcgroup.verCode) from MDCGroup _mdcgroup where 1=1 ");
		List<MDCGroup> mrList = getQueryChannelService().createJpqlQuery(jpql.toString()).list();
		return mrList;
	}
	@Override
	public List<Map<String, Object>> findAllMDC(MDCGroup entity) {
		// TODO Auto-generated method stub
		String sql = " select mdc.mdc_code,mdc.mdc_desc from drgs.t_mdcgroup mdc where mdc.ver_code = :verCode" ;
		SqlParameterSource paramSource = new BeanPropertySqlParameterSource(entity);
		List<Map<String,Object>> mdcList = jdbcTempSeverice.queryForList(sql, paramSource) ;
		return mdcList;
	}
	/**
	 *  @Description：保存mdc
	 *  @Author：liuhongjun 
	 *  @see com.dhcc.DRGsHosp.application.MDCGroupApplication#saveMdc(com.dhcc.DRGsHosp.core.domain.MDCGroup)
	 */
	@Override
	public void saveMdc(String code, String desc, String verCode) {
		StringBuilder sql = new StringBuilder();
		sql.append(" declare cnt INTEGER; begin \n");
		sql.append(" select count(*) into cnt from drgs.t_mdcgroup mdc \n");
		sql.append(" where mdc.mdc_code = '"+code+"' and mdc.ver_code = '"+verCode+"' ;\n");
		sql.append(" if cnt = 0 then \n");
		sql.append(" insert into drgs.t_mdcgroup (mdc_code,mdc_desc,ver_code) \n");
		sql.append(" values ('"+code+"','"+desc+"','"+verCode+"' );\n ");
		sql.append(" else \n ");
		sql.append(" update drgs.t_mdcgroup m set m.mdc_desc = '"+desc+"' \n ");
		sql.append(" where  m.ver_code = '"+verCode+"' and m.mdc_code = '"+code+"'; \n ");
		sql.append(" end if; \n ");
		sql.append("commit; \n ");
		sql.append("end; \n ");		
		jdbcTemplateObj.execute(sql.toString());	
	}
	/**
	 *  @Description：保存adrg
	 *  @Author：liuhongjun 
	 *  @see com.dhcc.DRGsHosp.application.MDCGroupApplication#saveMdc(com.dhcc.DRGsHosp.core.domain.MDCGroup)
	 */
	@Override
	public void saveAdrg(String code, String desc, String mdcCode, String verCode) {
		StringBuilder sql = new StringBuilder();
		sql.append(" declare cnt INTEGER; begin \n");
		sql.append(" select count(*) into cnt from drgs.t_adrg adrg \n");
		sql.append(" where adrg.adrg_code = '"+code+"' and adrg.ver_code = '"+verCode+"' ;\n");
		sql.append(" if cnt = 0 then \n");
		sql.append(" insert into drgs.t_adrg (adrg_code,adrg_desc,mdc_code,ver_code) \n");
		sql.append(" values ('"+code+"','"+desc+"','"+mdcCode+"','"+verCode+"' );\n ");
		sql.append(" else \n ");
		sql.append(" update drgs.t_adrg m set m.adrg_desc = '"+desc+"',m.mdc_code = '"+mdcCode+"' \n ");
		sql.append(" where  m.ver_code = '"+verCode+"' and m.adrg_code = '"+code+"'; \n ");
		sql.append(" end if; \n ");
		sql.append("commit; \n ");
		sql.append("end; \n ");		
		jdbcTemplateObj.execute(sql.toString());	
		
	}
	/**
	 *  @Description：保存drg
	 *  @Author：liuhongjun 
	 *  @see com.dhcc.DRGsHosp.application.MDCGroupApplication#saveDrg(java.lang.String, java.lang.String, char, java.lang.String)
	 */
	@Override
	public void saveDrg(String code, String desc, String adrgCode, String verCode) {
		StringBuilder sql = new StringBuilder();
		sql.append(" declare cnt INTEGER; begin \n");
		sql.append(" select count(*) into cnt from drgs.t_drgs drgs \n");
		sql.append(" where drgs.drg_code = '"+code+"' and drgs.ver_code = '"+verCode+"' ;\n");
		sql.append(" if cnt = 0 then \n");
		sql.append(" insert into drgs.t_drgs (drg_code,drg_desc,ver_code,adrg_code,pay_group) \n");
		sql.append(" values ('"+code+"','"+desc+"','"+verCode+"','"+adrgCode+"','0' );\n ");
		sql.append(" else \n ");
		sql.append(" update drgs.t_drgs m set m.drg_desc = '"+desc+"',m.adrg_code='"+adrgCode+"' \n ");
		sql.append(" where  m.ver_code = '"+verCode+"' and m.drg_code = '"+code+"'; \n ");
		sql.append(" end if; \n ");
		sql.append("commit; \n ");
		sql.append("end; \n ");		
		jdbcTemplateObj.execute(sql.toString());	
		
	}

}
